const errorType = require('../const/errorType')

// 异常处理   参数为错误类型
const errorHandle = (error,ctx) => {
  switch (error.message) {
        case errorType.ACCOUNT_NUMBER_IS_WRONG:
          ctx.status = 401;
          ctx.body = {
            code: 401,
            message: '用户名或密码已存在~'
          };
          break;
        case errorType.USER_IS_EXIST:
          ctx.status = 402;
          ctx.body = {
            code: 402,
            message: '用户名已存在~'
          };
          break;
        case errorType.PASSWORDS_IS_WRONG:
          ctx.status = 403;
          ctx.body = {
            code: 403,
            message: '用户名或密码错误~'
          };
          break;
        case errorType.USER_IS_NOT_EXIST:
          ctx.status = 404;
          ctx.body = {
            code: 404,
            message: '用户名不存在~'
          };
          break;
    case errorType.TOKEN_IS_NOT_EXIST:
          ctx.status = 405;
          ctx.body = {
            code: 405,
            message: 'token不存在~'
          };
          break;
    case errorType.PERMISSION_DENIED:
          ctx.status = 406;
          ctx.body = {
            code: 406,
            message: '权限不足~'
          };
          break;
        default:
          ctx.status = 500;
          ctx.body = {
            code: 500,
            message: '服务器错误~'
          };
          break;
    }
  console.log(error);
  return error;
}
module.exports = errorHandle;